package cn.zl.ymcc.controller;

import cn.zl.ymcc.entity.AccountFlow;
import cn.zl.ymcc.result.PageResult;
import cn.zl.ymcc.result.Result;
import cn.zl.ymcc.service.AccountFlowService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zl
 * @since 2025-10-18
 */
@RestController
@RequestMapping("/accountFlow")
public class AccountFlowController {

        @Autowired
        private AccountFlowService accountFlowService;

        @Operation(summary = "创建")
        @PostMapping
        public Result<Boolean> save(@RequestBody AccountFlow accountFlow) {
            boolean result = accountFlowService.save(accountFlow);
            return result ? Result.success() : Result.error("创建失败");
        }

        @Operation(summary = "更新")
        @PutMapping
        public Result<Boolean> update(@RequestBody AccountFlow accountFlow) {
            boolean result = accountFlowService.updateById(accountFlow);
            return result ? Result.success() : Result.error("更新失败");
        }

        @Operation(summary = "删除")
        @DeleteMapping("/{id}")
        public Result<Boolean> delete(
                @Parameter(description = "ID", required = true)
                @PathVariable Long id) {
            boolean result = accountFlowService.removeById(id);
            return result ? Result.success() : Result.error("删除失败");
        }

        @Operation(summary = "根据ID查询")
        @GetMapping("/{id}")
        public Result<AccountFlow> findById(
                @Parameter(description = "ID", required = true)
                @PathVariable Long id) {
            AccountFlow accountFlow = accountFlowService.getById(id);
            return accountFlow != null ? Result.success(accountFlow) : Result.error("数据不存在");
        }

        @Operation(summary = "查询所有")
        @GetMapping("/list")
        public Result<List<AccountFlow>> findAll() {
            List<AccountFlow> list = accountFlowService.list();
            return Result.success(list);
        }

        @Operation(summary = "分页查询")
        @GetMapping("/page")
        public Result<PageResult<AccountFlow>> findPage(
                @Parameter(description = "页码", example = "1")
                @RequestParam(defaultValue = "1") Integer pageNum,
                @Parameter(description = "每页数量", example = "10")
                @RequestParam(defaultValue = "10") Integer pageSize) {
            Page<AccountFlow> page = new Page<>(pageNum, pageSize);
            QueryWrapper<AccountFlow> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("id");

            IPage<AccountFlow> pageResult = accountFlowService.page(page, queryWrapper);
            PageResult<AccountFlow> result = PageResult.build(
                    pageResult.getTotal(),
                    pageResult.getRecords(),
                    pageNum,
                    pageSize
            );
            return Result.success(result);
        }

        }